package cn.moningxi.controller;


import cn.moningxi.bean.CCollect;
import cn.moningxi.bean.CustomerInfomation;
import cn.moningxi.service.CCollectService;
import cn.moningxi.service.GoodsInfomationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CCollectController {

    @Autowired
    private CCollectService cCollectService;
    @Autowired
    private GoodsInfomationService goodsInfomationService;

    /**
     * 收藏页面展示
     *
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping("cntApi/myCollection")
    public ModelAndView myCollection(ModelAndView mv, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        //查询当前用户所有的收藏信息
        List<CCollect> collectList = cCollectService.queryCcollect(onlineUser.getCustomerId());
        mv.addObject("goodsInfomationList", collectList);
        mv.setViewName("business/person/collection");
        return mv;
    }

    /**
     * 足迹展示
     *
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping("cntApi/myLookRecord")
    public ModelAndView myLookRecord(ModelAndView mv, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        //查询当前用户所有的收藏信息
        List<CCollect> collectList = cCollectService.queryCcollect(onlineUser.getCustomerId());
        mv.addObject("CCollectList", collectList);
        mv.setViewName("business/person/foot");
        return mv;
    }

    /**
     * 收藏页面取消收藏
     *
     * @param mv
     * @param collectId
     * @return
     */
    @RequestMapping("goodsIndexApi/saveCollect")
    public ModelAndView saveCollect(ModelAndView mv, Integer collectId) {
        cCollectService.saveCollect(collectId);
        mv.setViewName("redirect:/cntApi/myCollection");
        return mv;
    }

    /**
     * 商品页面添加、取消收藏
     *
     * @param mv
     * @param cCollect
     * @param request
     * @return
     */
    @RequestMapping("goodsIndexApi/modifyIsCancle")
    public ModelAndView modifyIsCancle(ModelAndView mv, CCollect cCollect, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        if (cCollect.getCollectId() == null) {
            cCollect.setCustomerId(onlineUser.getCustomerId());
            cCollectService.addCCollcet(cCollect);
        } else {
            cCollectService.modifyIsCancle(cCollect);
        }
        mv.setViewName("redirect:/goodsIndexApi/queryGoodsDetail?goodsId=" + cCollect.getGoodsId());
        return mv;
    }

    /**
     * 购物车添加收藏
     *
     * @param mv
     * @param cCollect
     * @param request
     * @return
     */
    @RequestMapping("goodsIndexApi/addCollect")
    public ModelAndView addCollect(ModelAndView mv, CCollect cCollect, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        if (cCollect.getCollectId() == null) {
            cCollect.setCustomerId(onlineUser.getCustomerId());
            cCollectService.addCCollcet(cCollect);
        } else {
            cCollectService.modifyIsCancle(cCollect);
        }
        mv.setViewName("redirect:/orderCartApi/queryAllShopCart");
        return mv;
    }

}
